from conn_mysql_redis import ConnRedis, ConnMysql, InsertConnMysql
from sanic import response
import uuid
import asyncio
import json

class UserLogin():

    async def user_login(self, phone, password):
        if not all((phone, password)):

            return {

                'status': -1,
                'errorCode': 21,
                'errorMsg': '请检查你的参数',
                'resultBody': None
            }

        sql = 'SELECT * from user where user_phone=%s and user_pwd=%s'
        result = await ConnMysql().select_one(sql, (phone, password))
       
        if result:

            access_token = uuid.uuid1().hex
            print(access_token)
            

            with await ConnRedis().pool_ as c:

                await c.hmset_dict(access_token, result)
                await c.expire(access_token,2*3600)
                return {
                        'status': 1, 
                        'errorCode': None,
                        'errorMsg': None,
                        'resultBody': {"info": "登录成功",
                                    "access_token": access_token}}
        else:

            return {
                'status': -1,
                'errorCode': 22,
                'errorMsg': '请检查你的用户名和密码',
                'resultBody': '登录失败'
            }